{#
    @allDomains     LDAP query result set
    @cur_domain     Current domain name.
#}

{% extends "layout.html" %}

{% from "macros/general.html" import
        display_subnav,
        display_input_cn, 
        display_reset_password,
        display_quota,
        with context
        %}
{% from "macros/msgHandlers.html" import userMsgHandler with context %}

{% block title %}{{ _('Add user') }}{% endblock title %}
{% block navlinks_active_create %}class="active"{% endblock %}

{% block breadcrumb %}
    {% set crumbs = [
            (ctx.homepath + '/domains', _('All domains'),),
            (ctx.homepath + '/profile/domain/general/' + cur_domain, cur_domain,),
            (ctx.homepath + '/users/' + cur_domain, _('Users'),),
            (None, _('Add user'),),
            ]
            %}
    {{ display_subnav(crumbs) }}
{% endblock %}

{% block main %}
{# Show system message #}
{{ userMsgHandler(msg) }}

{# -- Allow to create new account --#}
{% set createNewAccount = true %}

{% set numberOfSpareAccounts = profile.mailboxes - numberOfExistAccounts %}
{% set spareQuotaBytes = (profile.maxquota-usedQuotaSize)*1024*1024 %}

{% if profile.mailboxes > 0 and numberOfSpareAccounts <= 0 %}
    {% set createNewAccount = false %}
    {% set whyDisabledCreation = _('Already exceed max number of account limit (%s).') |format(profile.mailboxes |int) %}
{% endif %}

{% if profile.maxquota > 0 and spareQuotaBytes <= 0 %}
    {% set createNewAccount = false %}
    {% set whyDisabledCreation = _('No free domain quota left.') %}
{% endif %}

{# Display input field for adding new user. #}

{% if createNewAccount is sameas false %}
        <div class="notification note-error">
            <p>
                <strong>{{ _('Error:') }}</strong> {{ _('You can NOT create more users under domain %s.') |format('<a href="' + ctx.homepath + '/profile/domain/general/' + cur_domain + '"><strong>' + cur_domain + '</strong></a>') }}
                {{ whyDisabledCreation }}

                {# Show link to change limit #}
                {% if session.get('domainGlobalAdmin') is sameas true %}
                    <a href='{{ctx.homepath}}/profile/domain/advanced/{{cur_domain}}#domainQuota'>{{ _('Increase it now?') }}</a>
                {% endif %}
            </p>
        </div>
{% else %}
    {% if profile.mailboxes > 0 and numberOfSpareAccounts > 0 %}
            <div class="notification note-info">
                <p>{{ _('You can create <strong>%d</strong> more mail user(s) under domain %s.') |format(numberOfSpareAccounts, '<a href="' + ctx.homepath + '/profile/domain/general/' + cur_domain + '"><strong>' + cur_domain + '</strong></a>') }}</p>
            </div>
            <div class="bt-space0"></div>
    {% endif %}
{% endif %}

    <div class="content-box">
        <div class="box-body">
            <div class="box-header clear">
                <ul class="tabs clear">
                    <li><a href="{{ctx.homepath}}/users/{{cur_domain}}">{{ _('Users') }}</a></li>
                    <li class="active"><a href="#user_add"><img src="{{ctx.homepath}}/static/{{skin}}/images/ico_add_12.png" /> {{ _('User') }}</a>
                </ul>

                <h2>{{ _('Add user under domain:') }} {{cur_domain}}.</h2>
            </div>

            <div id="user_add" class="box-wrap clear">
                <form method="post" action="{{ctx.homepath}}/create/user/{{cur_domain}}">
                    <div class="form-field clear">
                        <h4 class="size-250 fl-space">{{ _('Mail Address') }} <span class="required">*</span></h4>
                        <span class="clean-padding">
                            <input type="text" size="35" name="username" value="" class="text fl-space" {% if createNewAccount is sameas false %}disabled{% endif %}/>@
                            <select name="domainName" onchange="changeUrl(this, baseurl='{{ctx.homepath}}/create/user/');">
                                {% for d in allDomains %}
                                    <option value="{{ d.domain |string }}" {% if d.domain == cur_domain %}selected{%endif%}>{{ d.domain }}</option>
                                {% endfor %}
                            </select>
                        </span>
                    </div>

                    <div class="bt-space5">&nbsp;</div>

                    {{ display_reset_password(
                                min_passwd_length=profile.minpasswordlength |string,
                                max_passwd_length=profile.maxpasswordlength |string,
                                )
                                }}

                    <div class="bt-space5">&nbsp;</div>

                    {{ display_input_cn(cn=cn, accountType='user') }}
                    {{ display_quota(
                                    value=profile.defaultuserquota,
                                    spare_quota_bytes=spareQuotaBytes,
                                    show_spare_quota='yes',
                                    show_value_in_input='yes',
                                    show_used_quota=false,
                                    )
                    }}

                <div class="rule2"></div>
                <div class="form-field clear">
                    <h4 class="size-250 fl-space">&nbsp;</h4>
                    <span>
                        <input type="submit" value="{{ _('Add') }}" {% if createNewAccount is sameas false %}class="button grey" disabled{% else %}class="button green"{% endif %} />
                    </span>
                </div>

            </form>
        </div>{# -- End box-wrap -- #}
    </div>{# -- End content-box -- #}
</div>{# -- End box-body -- #}
{% endblock main %}
